package sg.edu.nus.iss.club.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import sg.edu.nus.iss.club.Booking;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BookingPanel extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JList bookingList;
	private JLabel jLabel2;
	private JTextField dateField;
	private JButton removeBookingButton;
	private JLabel jLabel1;
	private JButton addBookingButton;
	public ClubApplication clubApplication;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String [] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new BookingPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
		System.out.println(frame.getHeight());
		System.out.println(frame.getWidth());
	}
	
	
	public BookingPanel() {
		super();
		initGUI();
	}
	
	public void refresh() {
		try {
			DateFormat df = new SimpleDateFormat(Booking.DATE_FORMAT);
			Date bookingDate =  df.parse(dateField.getText());
			Booking[] bookingListMData = clubApplication.getBookings(bookingDate);
			ListModel bookingListModel = 
				new DefaultComboBoxModel(bookingListMData);
			bookingList.setModel(bookingListModel);
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(null, "Illegal date format", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public class AddBookingActionListener implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			AddBookingDialog addBookingDialog = new AddBookingDialog(clubApplication, clubApplication.getMainWindow());
			addBookingDialog.setVisible(true);
			refresh();
		}
		
	}
	
	public String getDate() {
		return dateField.getText();
	}
	
	private void initGUI() {
		try {
			this.setLayout(null);
			this.setPreferredSize(new java.awt.Dimension(769, 199));
			this.setDoubleBuffered(false);
			{
				bookingList = new JList();
				this.add(bookingList, "0, 1, 2, 2");
				bookingList.setBounds(7, 34, 648, 147);
				bookingList.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				addBookingButton = new JButton();
				this.add(addBookingButton, "3, 1");
				addBookingButton.setText("Add");
				addBookingButton.setBounds(667, 34, 90, 27);
				addBookingButton.addActionListener(new AddBookingActionListener());
			}
			{
				removeBookingButton = new JButton();
				this.add(removeBookingButton, "3, 2");
				removeBookingButton.setText("Remove");
				removeBookingButton.setBounds(667, 67, 90, 27);
			}
			{
				JLabel alabel = new JLabel();
				this.add(alabel, "3, 2");
			}
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Bookings on");
				jLabel1.setBounds(6, 12, 90, 16);
			}
			{
				dateField = new JTextField();
				this.add(dateField);
				dateField.setBounds(87, 9, 81, 23);
				Date now = new Date();
				DateFormat df = new SimpleDateFormat(Booking.DATE_FORMAT);
				String dateStr = df.format(now);
				dateField.setText(dateStr);
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("(d-MMM-yyy)");
				jLabel2.setBounds(180, 12, 76, 16);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
